package com.bush.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.product.mapper.SpuInfoDescMapper;
import com.bush.mall.product.entity.SpuInfoDescEntity;
import com.bush.mall.product.service.SpuInfoDescService;

/**
 * spu信息介绍
 * @author RUN
 * @date 2022-11-28
 */
@Service(SpuInfoDescService.BEAN_ID)
@Slf4j
public class SpuInfoDescServiceImpl extends ServiceImpl<SpuInfoDescMapper, SpuInfoDescEntity> implements SpuInfoDescService {

    /**
     * 查询spu信息介绍列表
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<SpuInfoDescEntity> page = new Page<>(current,size);
            QueryWrapper<SpuInfoDescEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("SpuInfoDescService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建spu信息介绍表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result saveSpuInfoDesc(SpuInfoDescEntity spuInfoDesc) {
        try{
            Assert.notNull(spuInfoDesc,"参数异常");
            boolean flag = this.save(spuInfoDesc);
            if (flag) return Result.ok("spu信息介绍添加成功");
            throw new SaaSException("spu信息介绍添加失败");
        }catch(Exception e){
            log.error("SpuInfoDescService saveSpuInfoDesc ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改spu信息介绍表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result updateSpuInfoDesc(SpuInfoDescEntity spuInfoDesc) {
        try{
            Assert.notNull(spuInfoDesc,"参数异常");
            boolean flag = this.updateById(spuInfoDesc);
            if (flag) return Result.ok("spu信息介绍修改成功");
            throw new SaaSException("spu信息介绍修改失败");
        }catch(Exception e){
            log.error("SpuInfoDescService updateSpuInfoDesc ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除spu信息介绍表单
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result deleteSpuInfoDesc(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("spu信息介绍删除成功");
            throw new SaaSException("spu信息介绍删除失败");
        }catch(Exception e){
            log.error("SpuInfoDescService deleteSpuInfoDesc ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}